package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.RecipeAdditives;

public class RecipeAdditivesPresenter extends AbstractPresenter<RecipeAdditives> {
	final String SINGLEITEM = "recipeAdditives";
	final String MULTIITEM = "recipeAdditivess";
	
	public ArrayList<RecipeAdditives> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public RecipeAdditives tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public RecipeAdditives createItemFromJSONObject(JSONObject jsonObject){
		RecipeAdditives item = new RecipeAdditives();
		try{
			item.setId(jsonObject.getInt("additiveID"));
			item.setWeight(jsonObject.getDouble("additiveLbs"));
			item.setNotes(jsonObject.getString("additiveNotes"));
			item.setUnits(jsonObject.getString("additiveUnits"));
			
			final POSSupplyViewPresenter presenter = new POSSupplyViewPresenter();
			JSONObject jsonSC = jsonObject.getJSONObject("supply");
			item.setSupply(presenter.createItemFromJSONObject(jsonSC));
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(RecipeAdditives item){
		try{
			JSONObject json = new JSONObject();
			json.put("additiveID", item.getId());
			json.put("additiveLbs", item.getWeight());
			json.put("additiveNotes", item.getNotes());
			json.put("additiveUnits", item.getUnits());
			
			final POSSupplyViewPresenter presenter = new POSSupplyViewPresenter();
			json.put("supply", presenter.createJSON(item.getSupply()));

			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public RecipeAdditives getEmptyEntity(){
		RecipeAdditives entity = new RecipeAdditives();
		return entity;
	}
}
